package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ScoreListBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.view.adapter.MyIntegralAdapter;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.dialog.LoadingDialog;
import de.greenrobot.event.EventBus;

public class MyIntegralActivity extends BaseActivity implements View.OnClickListener {

    int page = 1;

    LinearLayoutManager linearLayoutManager;
    UserPresenter mUserPresenter;
    MyIntegralAdapter mMyIntegralAdapter;
    String userid;
    List<ScoreListBean.DataBean> mDataBeanList;
    LoadingDialog dialog;

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.recycleview)
    RecyclerView recycleview;
    @Bind(R.id.btn_integral_market)
    LinearLayout btnIntegralMarket;
    @Bind(R.id.refresh)
    SwipeRefreshLayout refresh;
    @Bind(R.id.textview_score)
    TextView textviewScore;
    @Bind(R.id.linearLayout_integral_info)
    LinearLayout linearLayoutIntegralInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_integral);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this);
        initList();
        initToolbar();
        initClickBtn();

        dialog = new LoadingDialog(this);
        dialog.show();

        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(this);
        userid = dataEntity.getUserid();

        mUserPresenter = new UserPresenterImp();
        mUserPresenter.getScoreList(page, userid);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constant.BACK_MY_INTEGRAL) {
            mDataBeanList.clear();
            mMyIntegralAdapter.notifyDataSetChanged();
            mUserPresenter.getScoreList(page, userid);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(final ScoreListBean bean) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        if (refresh.isRefreshing()) {
            refresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            mDataBeanList.addAll(bean.getData());
            mMyIntegralAdapter.notifyDataSetChanged();
            textviewScore.setText(bean.getScore());
            linearLayoutIntegralInfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MyIntegralActivity.this, IntegralExplainActivity.class);
                    intent.putExtra(Constant.TO_INTEGRAL_EXPLAIN_URL, bean.getUrl());
                    startActivity(intent);
                }
            });
        }
    }

    private void initClickBtn() {
        btnIntegralMarket.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_integral_market: {
                Intent intent = new Intent(this, IntegralChargeActivity.class);
                startActivityForResult(intent, Constant.BACK_MY_INTEGRAL);
            }
        }
    }

    private void initToolbar() {
        toolbar.setTitle("我的积分");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initList() {
        mDataBeanList = new ArrayList<>();
        mMyIntegralAdapter = new MyIntegralAdapter(mDataBeanList);

        linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recycleview.setHasFixedSize(true);
        recycleview.setLayoutManager(linearLayoutManager);
        recycleview.setAdapter(mMyIntegralAdapter);

        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mDataBeanList.clear();
                mMyIntegralAdapter.notifyDataSetChanged();

                page = 1;
                mUserPresenter.getScoreList(page, userid);
            }
        });
    }


}
